package cn.syw.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.syw.service.PermissionServiceImpl;
import cn.syw.service.iface.PermissionService;
/**
 * Servlet implementation class RoleServlet
 */
@WebServlet(
		urlPatterns = { "/permission.servlet" }, 
		initParams = { @WebInitParam(name = "char", value = "utf-8")})
public class PermissionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PermissionService permissionService = new PermissionServiceImpl();
	public String charset;
	
	public void init(ServletConfig config) {
		charset=config.getInitParameter("char");
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String ids = request.getParameter("ids");
		String params = request.getParameter("params");
		String param = request.getParameter("param");
		if(Objects.equals("init", param)) {
			List<Map<String,Object>> permissions = permissionService.queryPermission();
			request.setAttribute("permissions", permissions);
			request.getRequestDispatcher("views/permissionManager.jsp").forward(request, response);
		}
		if(Objects.equals("queryId", param)) {
			Map<String,Object> permission = permissionService.queryById(ids);
			request.setAttribute("permission", permission);
			request.setAttribute("edit", "on");
			request.getRequestDispatcher("views/permissionManager.jsp").forward(request, response);
		}
		if(Objects.equals("delete", param)) {
			permissionService.deletePermission(ids);
			out.println("<script>alert('删除成功！');window.location.href='permission.servlet?param=init'</script>");
		}
		if(Objects.equals("doUpdate", param)) {
			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
